package com.speedcar.base.result;

import com.speedcar.base.model.ScResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component
public class FlexibleResultWrapChecker {

    @Autowired
    private ScWrapDetectionProperties properties;

    public boolean isLikelyWrapped(Object body) {
        if (!properties.isSmartDetection()) {
            return body instanceof ScResultJson;
        }

        // 1. 明确的已知类型
        if (body instanceof ScResultJson) {
            return true;
        }

        // 2. Map类型检查
        if (body instanceof Map) {
            return isLikelyWrappedMap((Map<?, ?>) body);
        }

        // 3. 对象类型检查
        if (body != null) {
            return isLikelyWrappedObject(body);
        }

        return false;
    }

    private boolean isLikelyWrappedMap(Map<?, ?> map) {
        if (map.isEmpty()) {
            return false;
        }

        // 统计包含包装特征的字段数
        int wrapFieldCount = 0;
        for (Object key : map.keySet()) {
            if (key instanceof String) {
                String fieldName = (String) key;
                if (properties.getIndicators().contains(fieldName.toLowerCase())) {
                    wrapFieldCount++;
                }
            }
        }

        // 如果包含足够多的包装特征字段，则认为已包装
        return wrapFieldCount >= properties.getMinIndicators();
    }

    private boolean isLikelyWrappedObject(Object obj) {
        Class<?> clazz = obj.getClass();

        // 获取所有getter方法对应的字段名
        Set<String> fieldNames = new HashSet<>();
        for (Method method : clazz.getMethods()) {
            if (isGetterMethod(method)) {
                String fieldName = getFieldNameFromGetter(method.getName());
                if (fieldName != null) {
                    fieldNames.add(fieldName.toLowerCase());
                }
            }
        }

        // 统计包含包装特征的字段数
        int wrapFieldCount = 0;
        for (String fieldName : fieldNames) {
            if (properties.getIndicators().contains(fieldName)) {
                wrapFieldCount++;
            }
        }

        // 如果包含足够多的包装特征字段，则认为已包装
        return wrapFieldCount >= properties.getMinIndicators();
    }

    private boolean isGetterMethod(Method method) {
        return method.getName().startsWith("get") &&
                method.getParameterCount() == 0 &&
                !void.class.equals(method.getReturnType()) &&
                method.getDeclaringClass() != Object.class;
    }

    private String getFieldNameFromGetter(String methodName) {
        if (methodName.startsWith("get") && methodName.length() > 3) {
            String fieldName = methodName.substring(3);
            return Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1);
        } else if (methodName.startsWith("is") && methodName.length() > 2) {
            String fieldName = methodName.substring(2);
            return Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1);
        }
        return null;
    }
}
